//
//  CollectionViewCell.m
//  两种Cell间的切换
//
//  Created by 小果 on 16/7/14.
//  Copyright © 2016年 小果. All rights reserved.
//

#import "CollectionViewCell.h"
#import "CellModel.h"

@interface CollectionViewCell ()
@property (nonatomic, weak) UIImageView *imgView;
@property (nonatomic, weak) UILabel *name;
@property (nonatomic, weak) UILabel *dtail;

@end

@implementation CollectionViewCell
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor colorWithRed:0 green:255/255.0 blue:0 alpha:0.8];
        
        self.layer.cornerRadius = 20;
        self.layer.masksToBounds = YES;
        self.layer.borderColor = [UIColor yellowColor].CGColor;
        self.layer.borderWidth = 4;
        
        [self createSubViews];
    }
    return self;
}
#pragma mark - 创建Cell中的控件
-(void)createSubViews{
    
    UIImageView *imgView = [[UIImageView alloc] init];
    self.imgView = imgView;
    imgView.layer.cornerRadius = 10;
    imgView.layer.masksToBounds = YES;
    imgView.layer.borderWidth = 3;
    imgView.layer.borderColor = [UIColor purpleColor].CGColor;
    [self.contentView addSubview:imgView];
    
    UILabel *tlabel = [[UILabel alloc] init];
    tlabel.font = TnameFont;
    tlabel.textColor = [UIColor redColor];
    self.name = tlabel;
    [self.contentView addSubview:tlabel];
    
    UILabel *dLabel = [[UILabel alloc] init];
    dLabel.font = detailFont;
    self.dtail = dLabel;
    [self.contentView addSubview:dLabel];
    
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    CGSize cellSize = self.contentView.frame.size;
    
    // 图片的frame
    CGFloat iconW = 65;
    CGFloat iconH = 65;
    CGFloat iconX = (cellSize.width - iconW) * 0.5;
    CGFloat iconY = 5;
    self.imgView.frame = CGRectMake(iconX, iconY, iconW, iconH);
    
    // 标题的frame
    CGSize nameSize = [self.name.text sizeWithFont:TnameFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    CGFloat nameX = (cellSize.width - nameSize.width) * 0.5;
    CGFloat nameY = CGRectGetMaxY(self.imgView.frame) + 5;
    self.name.frame = CGRectMake(nameX, nameY, nameSize.width, nameSize.height);
    
    // 副标题的frame
    CGSize detailSize = [self.dtail.text sizeWithFont:detailFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    CGFloat detailX = (cellSize.width - detailSize.width) * 0.5;
    CGFloat detailY = CGRectGetMaxY(self.name.frame) + 2;
    self.dtail.frame = CGRectMake(detailX, detailY, detailSize.width, detailSize.height);
    
}

-(void)setCollModel:(CellModel *)collModel{
    _collModel = collModel;
    
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"1.gif" withExtension:nil];
    [self.imgView sd_setImageWithURL:url];
    
    self.name.text = collModel.name;
    
    self.dtail.text = collModel.detail;
    
}
@end
